package cn.sanch.pet.service.impl;

import cn.sanch.basic.loginContext.LoginContext;
import cn.sanch.basic.service.IBaiduAiAuditService;
import cn.sanch.basic.util.BusinessException;
import cn.sanch.org.domain.Employee;
import cn.sanch.org.mapper.EmployeeMapper;
import cn.sanch.pet.domain.Pet;
import cn.sanch.pet.domain.PetDetail;
import cn.sanch.pet.domain.PetOnlineAuditLog;
import cn.sanch.pet.mapper.PetDetailMapper;
import cn.sanch.pet.mapper.PetMapper;
import cn.sanch.pet.mapper.PetOnlineAuditLogMapper;
import cn.sanch.pet.service.IPetService;
import cn.sanch.basic.service.impl.BaseServiceImpl;
import cn.sanch.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void save(Pet pet) {
        super.save(pet);
        // 获取宠物明细对象
        PetDetail petDetail = pet.getPetDetail();
        // 如果对象不为空
        if (petDetail != null) {
            // 设置宠物id给宠物明细
            petDetail.setPetId(pet.getId());
            // 调用方法添加
            petDetailMapper.save(petDetail);
        }

    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        // 获取宠物明细对象
        PetDetail petDetail = pet.getPetDetail();
        // 如果对象不为空
        if (petDetail != null) {
            // 设置宠物id给宠物明细
            petDetail.setPetId(pet.getId());
            // 调用方法根据宠物id修改宠物明细对象
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    @Override
    public void delete(Long id) {
        super.delete(id);
        // 调用方法根据宠物id删除宠物明细对象
        petDetailMapper.deletePetDetailByPetId(id);
    }

    /**
     * 宠物批量上架
     * @param ids
     */
    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        // 1.调用工具类获取当前登录人的信息
        Logininfo logininfo = LoginContext.getLogininfo(request);

        // 2.通过logininfo获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        // 3.遍历ids
        for (Long id : ids) {
            // 4.获取每一个宠物对象，调用百度Ai对宠物名字和图片进行审核
            Pet pet = petMapper.findOne(id);
            // 4.1审核宠物名字
            Boolean textBoo = baiduAiAuditService.textAudit(pet.getName());
            // 4.2审核宠物图片
            Boolean imageBoo = baiduAiAuditService.imageAudit(pet.getResources());

            // 5.如果百度Ai都审核通过
            if (textBoo && imageBoo) {
                // 6.修改状态+添加宠物上架审核通过日志
                // 6.1修改宠物状态
                pet.setState(1);
                // 6.2修改上架时间
                pet.setOnsaletime(new Date());
                petMapper.update(pet);

                // 6.3添加宠物上架审核通过日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                // -1表示审核不通过 0表示待审核 1表示审核通过
                log.setState(1);
                log.setNote("宠物上架审核成功！");
                log.setAuditTime(new Date());
                // 审核人
                log.setAuditId(employee.getId());

                petOnlineAuditLogMapper.save(log);

                // 7.百度Ai审核不通过
            }else {
               // 8添加宠物上架审核失败日志
                // 8.1添加宠物上架审核不通过日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                // -1表示审核不通过 0表示待审核 1表示审核通过
                log.setState(-1);
                log.setNote("宠物上架审核失败，名字或图片违规！");
                log.setAuditTime(new Date());
                // 审核人
                log.setAuditId(employee.getId());

                petOnlineAuditLogMapper.save(log);

                throw new BusinessException("宠物上架审核失败，名字或图片违规！");
            }

        }

    }

    /**
     * 宠物批量下架
     * @param ids
     */
    @Override
    public void offsale(Long[] ids) {
        // 创建一个当前系统时间
        Date offsaletime = new Date();
        petMapper.offsale(ids, offsaletime);
    }
}
